package cn.bs.busuo.content.service.impl;

import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.content.dao.persist.repository.impl.TagRepositoryImpl;
import cn.bs.busuo.content.pojo.entity.Tag;
import cn.bs.busuo.content.pojo.param.TagAddNewParam;
import cn.bs.busuo.content.service.TagService;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Feng
 * @since: 2023/12/10
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepositoryImpl tagRepository;

    @Override
    public void addNewTag(TagAddNewParam tagAddNewParam) {
        Tag tag = new Tag();
        BeanUtil.copyProperties(tagAddNewParam, tag);
        int i = tagRepository.insert(tag);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"添加标签失败");
        }
    }

    @Override
    public void deleteTag(List<Long> ids) {
        int i = tagRepository.deleteByIds(ids);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"删除标签失败");
        }
    }

    @Override
    public String selectTagList() {
        List<Tag> tags = tagRepository.selectTagList();
        if (tags.size() == 0){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有性格标签");
        }
        String json = JSON.toJSON(tags).toString();
        return json;
    }
}
